﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using QldtSdh.DAL.Models;

namespace QldtSdh.BLL
{
    public partial class BizKhoaHocNCS
    {
        /// <summary>
        /// Lấy danh sách
        /// </summary>
        /// <returns></returns>
        public static List<KhoaHocNCS> GetList(CEMS4UG_DHKHContext dbContext=null)
        {
            using (var db = CEMS4UG_DHKHContext.GetContext())
            {
                return db.KhoaHocNCSDbSet.OrderByDescending(e => e.NamTuyenSinh).ThenBy(e => e.TenKhoaHoc).ToList();
            }
        }

        public static KhoaHocNCS GetByID(string maKhoaHocNCSGUID)
        {
            using (var db = CEMS4UG_DHKHContext.GetContext())
            {
                return db.KhoaHocNCSDbSet.Where(e => e.MaKhoaHocNCSGUID == maKhoaHocNCSGUID).FirstOrDefault();
            }
        }

        public static CODE_RESULT_RETURN Add(KhoaHocNCS obj)
        {
            using (var db = CEMS4UG_DHKHContext.GetContext())
            {
                var khoaHocNCS = db.KhoaHocNCSDbSet.Where(e => e.TenKhoaHoc == obj.TenKhoaHoc || e.NamTuyenSinh == obj.NamTuyenSinh).FirstOrDefault();
                if (khoaHocNCS != null)
                    return CODE_RESULT_RETURN.TenTrung;
                obj.MaKhoaHocNCSGUID = Guid.NewGuid().ToString();
                db.KhoaHocNCSDbSet.Add(obj);
                db.SaveChanges();
                NhatKyHoatDong nhatKy = new NhatKyHoatDong();
                BizNhatKyHoatDong.InitData<KhoaHocNCS>(nhatKy, null, obj.MaKhoaHocNCSGUID, LOAIDOITUONG.KHÓA_HỌC_NCS, LOAITHAOTAC.Thêm);
                BizNhatKyHoatDong.Add<KhoaHocNCS>(nhatKy, obj);
                return CODE_RESULT_RETURN.ThanhCong;
            }
        }

        public static CODE_RESULT_RETURN Update(KhoaHocNCS obj)
        {
            using (var db = CEMS4UG_DHKHContext.GetContext())
            {
                var _kh = db.KhoaHocNCSDbSet.Where(e => (e.TenKhoaHoc == obj.TenKhoaHoc || e.NamTuyenSinh == obj.NamTuyenSinh) && e.MaKhoaHocNCSGUID != obj.MaKhoaHocNCSGUID).FirstOrDefault();
                if (_kh != null)
                    return CODE_RESULT_RETURN.TenTrung;
                _kh = db.KhoaHocNCSDbSet.Where(e => e.MaKhoaHocNCSGUID == obj.MaKhoaHocNCSGUID).FirstOrDefault();
                if (_kh == null)
                    return CODE_RESULT_RETURN.ThatBai;
                NhatKyHoatDong nhatKy = new NhatKyHoatDong();
                BizNhatKyHoatDong.InitData<KhoaHocNCS>(nhatKy, _kh, obj.MaKhoaHocNCSGUID, LOAIDOITUONG.KHÓA_HỌC_NCS, LOAITHAOTAC.Chỉnh_sửa);

                _kh.TenKhoaHoc = obj.TenKhoaHoc;
                _kh.NamTuyenSinh = obj.NamTuyenSinh;
                db.SaveChanges();
                BizNhatKyHoatDong.Add<KhoaHocNCS>(nhatKy, _kh);
                return CODE_RESULT_RETURN.ThanhCong;
            }
        }

        public static CODE_RESULT_RETURN Delete(KhoaHocNCS obj)
        {
            using (var db = CEMS4UG_DHKHContext.GetContext())
            {
                var _kh = db.KhoaHocNCSDbSet.Where(e => e.MaKhoaHocNCSGUID == obj.MaKhoaHocNCSGUID).FirstOrDefault();
                if (_kh == null)
                    return CODE_RESULT_RETURN.ThatBai;
                if (_kh.ListNghienCuuSinh != null && _kh.ListNghienCuuSinh.Count > 0)
                    return CODE_RESULT_RETURN.ThatBai;
                db.KhoaHocNCSDbSet.Remove(_kh);
                db.SaveChanges();
                NhatKyHoatDong nhatKy = new NhatKyHoatDong();
                BizNhatKyHoatDong.InitData<KhoaHocNCS>(nhatKy, obj, obj.MaKhoaHocNCSGUID, LOAIDOITUONG.KHÓA_HỌC_NCS, LOAITHAOTAC.Xóa);
                BizNhatKyHoatDong.Add<KhoaHocNCS>(nhatKy, null);
                return CODE_RESULT_RETURN.ThanhCong;
            }
        }
    }

}
